home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / C++ / Direct3D / Instancing / Instancing.fx < prev    next >
Encoding:
Text File  |  2004-09-29  |  8.0 KB  |  209 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: Instancing.fx
  3. //
  4. // The effect file for the Instancing sample.  
  5. // 
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8.  
  9.  
  10. //--------------------------------------------------------------------------------------
  11. // Global variables
  12. //--------------------------------------------------------------------------------------
  13. texture g_txScene : TEXTURE;        // texture for scene rendering
  14. float4x4 g_mWorld : WORLD : register(c0); // World matrix for object
  15. float4x4 g_mView : VIEW : register(c4);            // World matrix for object
  16. float4x4 g_mProj : PROJECTION : register(c8);        // World matrix for object
  17.  
  18. // only used for Constants instancing
  19. float4 g_BoxInstance_Position : BOXINSTANCE_POSITION : register(c13);
  20. float4 g_BoxInstance_Color : BOXINSTANCE_COLOR : register(c14);
  21.  
  22. // only used for vs_2_0 Shader instancing
  23. #define g_nNumBatchInstance 120
  24. float4 g_vBoxInstance_Position[g_nNumBatchInstance] : BOXINSTANCEARRAY_POSITION : register(c16);
  25. float4 g_vBoxInstance_Color[g_nNumBatchInstance] : BOXINSTANCEARRAY_COLOR : register(c136);
  26.  
  27. //-----------------------------------------------------------------------------
  28. // Texture samplers
  29. //-----------------------------------------------------------------------------
  30. sampler g_samScene =
  31. sampler_state
  32. {
  33.     Texture = <g_txScene>;
  34.     MinFilter = Linear;
  35.     MagFilter = Linear;
  36.     MipFilter = Linear;
  37. };
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Name: VS_HWInstancing
  41. // Type: Vertex shader (HW Instancing)
  42. // Desc: This shader computes standard transform and lighting for unlit, texture-mapped triangles.
  43. //-----------------------------------------------------------------------------
  44. void VS_HWInstancing( float4 vPos : POSITION,
  45.                     float3 vNormal : NORMAL,
  46.                     float2 vTex0 : TEXCOORD0,
  47.                     float4 vColor : COLOR0,
  48.                     float4 vBoxInstance : COLOR1,
  49.                     out float4 oPos : POSITION,
  50.                     out float4 oColor : COLOR0,
  51.                     out float2 oTex0 : TEXCOORD0 )
  52. {
  53.     //Use the fourth component of the vBoxInstance to rotate the box:
  54.     vBoxInstance.w *= 2 * 3.1415;
  55.     float4 vRotatedPos = vPos;
  56.     vRotatedPos.x = vPos.x * cos(vBoxInstance.w) + vPos.z * sin(vBoxInstance.w);
  57.     vRotatedPos.z = vPos.z * cos(vBoxInstance.w) - vPos.x * sin(vBoxInstance.w);
  58.     
  59.     //Use the instance position to offset the incoming box corner position:
  60.     //  The "* 32 - 16" is to scale the incoming 0-1 intrapos range so that it maps to 8 box widths, covering
  61.     //  the signed range -8 to 8. Boxes are 2 word units wide.
  62.     vRotatedPos += float4( vBoxInstance.xyz * 32 - 16, 0 );
  63.     
  64.     // Transform the position from object space to homogeneous projection space
  65.     oPos = mul( vRotatedPos, g_mWorld );
  66.     oPos = mul( oPos, g_mView );
  67.     oPos = mul( oPos, g_mProj );
  68.     
  69.     // Just copy the texture coordinate & color through
  70.     oTex0 = vTex0;
  71.     oColor = vColor;
  72. }
  73.  
  74.  
  75. //-----------------------------------------------------------------------------
  76. // Name: VS_ShaderInstancing
  77. // Type: Vertex shader (Shader Instancing)
  78. // Desc: This shader computes standard transform and lighting for unlit, texture-mapped triangles.
  79. //-----------------------------------------------------------------------------
  80. void VS_ShaderInstancing( float4 vPos : POSITION,
  81.                         float3 vNormal : NORMAL,
  82.                         float2 vTex0 : TEXCOORD0,
  83.                         float vBoxInstanceIndex : TEXCOORD1,
  84.                         out float4 oPos : POSITION,
  85.                         out float4 oColor : COLOR0,
  86.                         out float2 oTex0 : TEXCOORD0 )
  87. {
  88.     // Use the fourth component of the vBoxInstance to rotate the box:
  89.     float4 vBoxInstance = g_vBoxInstance_Position[vBoxInstanceIndex];
  90.     vBoxInstance.w *= 2 * 3.1415;
  91.     float4 vRotatedPos = vPos;
  92.     vRotatedPos.x = vPos.x * cos(vBoxInstance.w) + vPos.z * sin(vBoxInstance.w);
  93.     vRotatedPos.z = vPos.z * cos(vBoxInstance.w) - vPos.x * sin(vBoxInstance.w);
  94.     
  95.     //Use the instance position to offset the incoming box corner position
  96.     //  The "* 32 - 16" is to scale the incoming 0-1 intrapos range so that it maps to 8 box widths, covering
  97.     //  the signed range -8 to 8. Boxes are 2 word units wide.
  98.     vRotatedPos += float4( vBoxInstance.xyz * 32 - 16, 0 );
  99.     
  100.     // Transform the position from object space to homogeneous projection space
  101.     oPos = mul( vRotatedPos, g_mWorld );
  102.     oPos = mul( oPos, g_mView );
  103.     oPos = mul( oPos, g_mProj );
  104.     
  105.     // Just copy the texture coordinate & color through
  106.     oTex0 = vTex0;
  107.     oColor = g_vBoxInstance_Color[vBoxInstanceIndex];
  108. }
  109.  
  110.  
  111. //-----------------------------------------------------------------------------
  112. // Name: VS_ConstantsInstancing
  113. // Type: Vertex shader (Constants Instancing)
  114. // Desc: This shader computes standard transform and lighting for unlit, texture-mapped triangles.
  115. //-----------------------------------------------------------------------------
  116. void VS_ConstantsInstancing( float4 vPos : POSITION,
  117.                             float3 vNormal : NORMAL,
  118.                             float2 vTex0 : TEXCOORD0,
  119.                             out float4 oPos : POSITION,
  120.                             out float4 oColor : COLOR0,
  121.                             out float2 oTex0 : TEXCOORD0 )
  122. {
  123.     // Use the fourth component of the vBoxInstance to rotate the box:
  124.     float4 vBoxInstance = g_BoxInstance_Position;
  125.     vBoxInstance.w *= 2 * 3.1415;
  126.     float4 vRotatedPos = vPos;
  127.     vRotatedPos.x = vPos.x * cos(vBoxInstance.w) + vPos.z * sin(vBoxInstance.w);
  128.     vRotatedPos.z = vPos.z * cos(vBoxInstance.w) - vPos.x * sin(vBoxInstance.w);
  129.     
  130.     // Use the instance position to offset the incoming box corner position
  131.     //  The "* 32 - 16" is to scale the incoming 0-1 intrapos range so that it maps to 8 box widths, covering
  132.     //  the signed range -8 to 8. Boxes are 2 word units wide.
  133.     vRotatedPos += float4( vBoxInstance.xyz * 32 - 16, 0 );
  134.     
  135.     // Transform the position from object space to homogeneous projection space
  136.     oPos = mul( vRotatedPos, g_mWorld );
  137.     oPos = mul( oPos, g_mView );
  138.     oPos = mul( oPos, g_mProj );
  139.     
  140.     // Just copy the texture coordinate & color through
  141.     oTex0 = vTex0;
  142.     oColor = g_BoxInstance_Color;
  143. }
  144.  
  145.  
  146. //-----------------------------------------------------------------------------
  147. // Name: PS
  148. // Type: Pixel shader
  149. // Desc: This shader outputs the pixel's color by modulating the texture's
  150. //         color with diffuse material color
  151. //-----------------------------------------------------------------------------
  152. float4 PS(    float2 vTex0 : TEXCOORD0,
  153.             float4 vColor : COLOR0 ) : COLOR0
  154. {
  155.     // Lookup texture and modulate it with diffuse
  156.     return tex2D( g_samScene, vTex0 ) * vColor;
  157. }
  158.  
  159.  
  160. //-----------------------------------------------------------------------------
  161. // Name: THW_Instancing
  162. // Type: Technique
  163. // Desc: Renders scene through Hardware instancing
  164. //-----------------------------------------------------------------------------
  165. technique THW_Instancing
  166. {
  167.     pass P0
  168.     {
  169.         VertexShader = compile vs_2_0 VS_HWInstancing();
  170.         PixelShader  = compile ps_1_1 PS();
  171.         ZEnable = true;
  172.         AlphaBlendEnable = false;
  173.     }
  174. }
  175.  
  176.  
  177. //-----------------------------------------------------------------------------
  178. // Name: TShader_Instancing
  179. // Type: Technique
  180. // Desc: Renders scene through Shader instancing (batching)
  181. //-----------------------------------------------------------------------------
  182. technique TShader_Instancing
  183. {
  184.     pass P0
  185.     {
  186.         VertexShader = compile vs_2_0 VS_ShaderInstancing();
  187.         PixelShader  = compile ps_1_1 PS();
  188.         ZEnable = true;
  189.         AlphaBlendEnable = false;
  190.     }
  191. }
  192.  
  193.  
  194. //-----------------------------------------------------------------------------
  195. // Name: TConstants_Instancing
  196. // Type: Technique
  197. // Desc: Renders scene through Constants instancing (no batching)
  198. //-----------------------------------------------------------------------------
  199. technique TConstants_Instancing
  200. {
  201.     pass P0
  202.     {
  203.         VertexShader = compile vs_2_0 VS_ConstantsInstancing();
  204.         PixelShader  = compile ps_1_1 PS();
  205.         ZEnable = true;
  206.         AlphaBlendEnable = false;
  207.     }
  208. }
  209.